<template>
  <a-modal :open="open" width="640px" :title="title" centered ok-text="保存" @ok="onOk" @cancel="onCancel">
    <a-form class="form-style-flex" name="validate_other" :model="formState">
      <a-form-item label="出库仓库" class="w-1/2">
        {{ formState.ksmc }}
      </a-form-item>
      <a-form-item label="制单人" class="w-1/2">
        {{ formState.applyByName }}
      </a-form-item>
      <a-form-item label="备注" class="w-full mb-3">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

import useUserStore from '@/store/user'

const { empId, empName } = useUserStore()

const emit = defineEmits(['update:open', 'update:value', 'success'])

const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  },
  title: {
    type: String,
    default: ''
  }
})

const formState = ref({
  ksmc: '',
  ksdm: '',
  applyByName: '',
  applyBy: '',
  memo: ''
})
watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData }
      getDepartInfo()
    }
  }
)
const getDepartInfo = async () => {
  const res = await post('/common/masterData/getByYsbhDepartment', { empId: empId })
  if (res.code !== 0) message.error(res.msg || '获取登录人科室失败')
  formState.value.ksmc = res.data?.ksmc
  formState.value.ksdm = res.data?.ksdm
  formState.value.applyByName = empName
  formState.value.applyBy = empId
}

const onOk = async () => {
  const res = await post('/materials/consume/doSaveMtrConsume', {
    ...formState.value,
    storeCode: formState.value.ksdm,
    storeName: formState.value.ksmc
  })
  if (res.code !== 0) message.error(res.msg || '保存失败')
  message.success(res.msg)
  resetFields()
  emit('update:open', false)
  emit('success', res.data)
}
const resetFields = () => {
  formState.value = {
    ksmc: '',
    ksdm: '',
    applyByName: '',
    applyBy: '',
    memo: ''
  }
}
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
</script>
<style lang="less" scoped></style>
